package com.menu.common.utils.http;

import com.alibaba.fastjson2.JSONObject;

import org.springframework.http.*;

import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 *
 * @author wac
 * @date 2024/7/22
 * @Description
 */public class DwtHttpUtils {

     private  static String URL="http://47.98.173.42:8080/";
    public static String RestTemplateTestPost(String index, JSONObject paramMap,String token) {
        String url = URL.concat(index);

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.set("user-agent", "Mozilla/5.0 WindSnowLi-Blog");
        if(token != null){
            headers.add("token", token);
        }
        //        整合请求头和请求参数
        HttpEntity<JSONObject> httpEntity = new HttpEntity<>(paramMap, headers);
        //         请求客户端
        RestTemplate client = new RestTemplate();
        //      发起请求
        JSONObject body = client.postForEntity(url, httpEntity, JSONObject.class).getBody();
        System.out.println("******** POST请求 *********");
        assert body != null;
        return body.toJSONString();
    }
    public static String RestTemplateTestGet(String index, Map<String, Object> paramMap,String token) {
        String url = URL.concat(index);
        HttpHeaders headers = new HttpHeaders();

        if(token != null){
            headers.add("token", token);
        }
        //         请求客户端
        RestTemplate client = new RestTemplate();



        //      发起请求
        String body = client.getForEntity(url, String.class,headers).getBody();
        System.out.println("******** Get请求 *********");
        assert body != null;


        HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<MultiValueMap<String, Object>>(null,headers);
        ResponseEntity<String> response2 = client.exchange(url, HttpMethod.GET, httpEntity, String.class,paramMap);
//        JSONObject jsonObject=JSONObject.parseObject(response2.getBody());
//        List<String> data = jsonObject.getJSONArray("data").stream().map(t -> ((JSONObject) t).getString("typeName")).collect(Collectors.toList());
        return response2.getBody();
    }


}
